#include <stdio.h>
#include <memory.h>
#include "platform.h"
#include "devvideo.h"


int main(void)
{
    DevVideoConfig ov5640Config, pxpConfig;
    DevVideo ov5640, pxp;
    int i;

    {/* ov5640 */
        /* configure */
        memset(&ov5640Config, 0, sizeof(DevVideoConfig));
        ov5640Config.input = 0;
        ov5640Config.output = -1;
        ov5640Config.bufferNumber = 4;
        ov5640Config.node = "/dev/video0";
        ov5640Config.vcrop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        ov5640Config.vcrop.c.height = 480;
        ov5640Config.vcrop.c.left = 0;
        ov5640Config.vcrop.c.top = 0;
        ov5640Config.vcrop.c.width = 640;
        ov5640Config.vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        ov5640Config.vfmt.fmt.pix.width = 640;
        ov5640Config.vfmt.fmt.pix.height = 480;
        ov5640Config.vfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
        /* initialize */
        if (DevVideo_initialize(&ov5640, &ov5640Config) < 0) {
            printf("DevVideo_initialize ov5640 error\n");
            return -1;
        }
    }

    {/* pxp */
        /* configure */
        memset(&pxpConfig, 0, sizeof(DevVideoConfig));
        pxpConfig.input = -1;
        pxpConfig.output = 0;
        pxpConfig.bufferNumber = 4;
        pxpConfig.node = "/dev/video1";
        pxpConfig.vcrop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
        pxpConfig.vcrop.c.height = 480;
        pxpConfig.vcrop.c.left = 0;
        pxpConfig.vcrop.c.top = 0;
        pxpConfig.vcrop.c.width = 640;
        pxpConfig.vfmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
        pxpConfig.vfmt.fmt.pix.width = 640;
        pxpConfig.vfmt.fmt.pix.height = 480;
        pxpConfig.vfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
        /* initialize */
        if (DevVideo_initialize(&pxp, &pxpConfig) < 0) {
            printf("DevVideo_initialize pxp error\n");
            return -1;
        }

    }

    {/* start */
        if (DevVideo_streamOn(&ov5640) < 0) {
            printf("DevVideo_streamOn error");
            goto out;
        }
        if (DevVideo_streamOn(&pxp) < 0) {
            printf("DevVideo_streamOn pxp error");
            goto out;
        }
        for (i = 0; i < 30000; i++) {
            struct v4l2_buffer bufferCapture, bufferPxpIn;
            if (DevVideo_dqBuffer(&ov5640, &bufferCapture) < 0) {
                printf("DevVideo_dqBuffer: ov5640 error\n");
                break;
            }
            if (DevVideo_dqBuffer(&pxp, &bufferPxpIn) < 0) {
                printf("DevVideo_dqBuffer: pxp error\n");
                break;
            }
            printf("get a frame %d\n", i);
            memcpy(pxp.buffer[bufferPxpIn.index].start,
                    ov5640.buffer[bufferCapture.index].start, ov5640.buffer[bufferCapture.index].length);
            if (DevVideo_qBuffer(&ov5640, &bufferCapture) < 0) {
                printf("DevVideo_qBuffer: ov5640 error\n");
                break;
            }
            if (DevVideo_qBuffer(&pxp, &bufferPxpIn) < 0) {
                printf("DevVideo_qBuffer: pxp error\n");
                break;
            }
        }
    }


    /* out */
out:
    DevVideo_streamOff(&ov5640);
    DevVideo_uninitialize(&ov5640);
    return 0;
}











